import common from '@/common/common.js'
// 路由封装
import route from '@/uni_modules/uview-ui/libs/util/route.js'
//导入音频
import audioUtil from '@/common/util/audioUtil.js'


/**手机号截取
 * @param {Object} phone
 */
function phoneSub(phone){
	return phone.substr(0, 3) + '****' + phone.substr(7, 11);
}

/**字符串截取
 * @param {Object} phone
 */
function strSub(str,num,push="..."){
	return str.substr(0, num) + push;
}
/**
 * 防抖debounce函数
 * @param {*} fn 要防抖的函数
 * @param {*} delay 要延迟的时间
 */
export function debounce(fn, delay = 500) {
	let timeout; // 创建一个标记用来存放定时器的返回值
	return function() {
		// 每当用户输入的时候把前一个 setTimeout clear 掉
		if (timeout) {
			clearTimeout(timeout);
		}
		// 然后又创建一个新的 setTimeout, 这样就能保证interval 间隔内如果时间持续触发，就不会执行 fn 函数
		timeout = setTimeout(() => {
			fn.apply(this, arguments);
		}, delay);
	};
};

/**
 * 时间戳转化为年 月 日 时 分 秒
 * ts: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
function tsFormatTime(timestamp, format) {
  // timestamp = timestamp.replace("T", " ");
  // timestamp = new Date(timestamp.replace(/-/g, '/'));
  const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  let returnArr = [];
  let date = new Date(timestamp?timestamp.replace(/-/g, '/'):timestamp);

  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  returnArr.push(year, month, day, hour, minute, second);

  returnArr = returnArr.map(formatNumber);

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;

}


/**获取当前时间
 * @param {Object} format
 */
function getCurrTime(format='Y-M-D h:m:s'){
	const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	let returnArr = [];
	let date =  new Date();
	let year = date.getFullYear()
	let month = date.getMonth() + 1
	let day = date.getDate()
	let hour = date.getHours()
	let minute = date.getMinutes()
	let second = date.getSeconds()
	returnArr.push(year, month, day, hour, minute, second);
	
	returnArr = returnArr.map(formatNumber);
	
	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**秒数格式化时分秒
 * @param {Object} value
 */
function formatSeconds(value) {
    let result = parseInt(value)
    let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
    let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
    let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
 
    let res = '';
    if(h !== '00') res += `${h}h`;
    if(m !== '00') res += `${m}min`;
    res += `${s}s`;
    return res;
}

/**路由跳转
 * @param {Object} e
 * @param {Object} id
 */
function navto(e, params={},type='navigateTo',delta=1) {
	let url = '/pages/' + e;
	route({
		type:type,
		delta:delta,
		url: url,
		params: params
	})
}
// 使用示例  tyep : navigateTo或to对应uni.navigateTo，redirect或redirectTo对应uni.redirectTo，switchTab或tab对应uni.switchTab，reLaunch对应uni.reLaunch，navigateBack或back对应uni.navigateBack
// $util.navto('auth/forgot-password/forgot-password')
//$util.navto('auth/forgot-password/forgot-password',{name: 'lisa', age: 18})
//$util.navto('',{},'back',1)


//根据value获取label
function findValueLabel(value,arr) {
  let label = '';
  arr.find((k) => {
    if(k['value'] == value){
      label = k['label'];
    }
  })
  return label;
}


export default {
	phoneSub,
	strSub,
	tsFormatTime,
	navto,
	findValueLabel,
	getCurrTime,
	formatSeconds,
	debounce
}
